package com.aube.plugin.figure;

import android.animation.ValueAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.aube.plugin.origin.R;
import com.huyn.baseframework.ImageLoader;
import com.huyn.baseframework.ImageSizeDefineUtil;
import com.huyn.baseframework.dynamicload.DLLayout;
import com.huyn.baseframework.dynamicload.IPositionManager;
import com.huyn.baseframework.model.Character;
import com.huyn.baseframework.utils.SysUtil;
import com.ryg.dynamicload.internal.DLIntent;

import java.util.List;

/**
 * Created by huyaonan on 16/5/23.
 */
public class FigureView extends DLLayout implements IFigureView {

    private int mSpace=0;
    private int mItemSpace = 0;
    private int mScreenWidth = 0;

    private int mFirstLineWidth = 0;
    private int mSecondLineWidth = 0;

    public FigureView(Context context) {
        this(context, null);
    }
    public FigureView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }
    public FigureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void initViews() {
        mSpace = getResources().getDimensionPixelOffset(R.dimen.widget_margin);
        mItemSpace = mSpace / 3;
        mScreenWidth = SysUtil.getAbsoluteScreenWidth(context);

        mFirstLineWidth = (mScreenWidth - 2 * mSpace - mItemSpace) / 2;
        mSecondLineWidth = (mScreenWidth - 2 * mSpace - 3 * mItemSpace) / 4;
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int height = mFirstLineWidth*4/5 + mItemSpace + mSecondLineWidth*4/3;
        super.onMeasure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY));
    }

    @Override
    protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
        for (int i = 0; i < getChildCount(); i++) {
            if(i < 2) {
                int left = i * (mFirstLineWidth + mItemSpace) + mSpace;
                getChildAt(i).layout(left, 0, left + mFirstLineWidth, mFirstLineWidth*4/5);
            } else {
                int left = (i-2) * (mSecondLineWidth + mItemSpace) + mSpace;
                int top =  mFirstLineWidth*4/5 + mItemSpace;
                getChildAt(i).layout(left, top, left + mSecondLineWidth, top + mSecondLineWidth*4/3);
            }
        }
    }

    public void anim(int delay) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount ; i++) {
            int x = i > 0 ? 1 : 0;
            int y = i >= 3 ? (1 + i%3) : 0;

            final int mItemWidth = (i == 0 || i == 1) ? mFirstLineWidth : mSecondLineWidth;

            final View view = getChildAt(i);
            view.setAlpha(0f);
            final ValueAnimator anim = ValueAnimator.ofFloat(0, 1);
            anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    float value = (float) animation.getAnimatedValue();

                    view.setAlpha(value);
                    view.setTranslationX(mItemWidth*(1-value));
                }
            });
            anim.setDuration(300);
            anim.setStartDelay(delay+200*(x+y));
            anim.start();
        }
    }

    @Override
    public void fillData(Object o) {
        if(o == null || !(o instanceof List))
            return;
        List list = (List) o;
        int size = list.size();
        for(int i=0; i<size; i++) {
            final Character character = (Character) list.get(i);
            final ImageView img = new ImageView(context);
            img.setScaleType(ImageView.ScaleType.CENTER_CROP);

            int mItemWidth=0;
            int mItemHeight=0;

            if(i == 0 || i == 1) {
                mItemWidth = mFirstLineWidth;
                mItemHeight = mFirstLineWidth*4/5;
            } else {
                mItemWidth = mSecondLineWidth;
                mItemHeight = mSecondLineWidth*4/3;
            }
            addView(img, new ViewGroup.LayoutParams(mItemWidth, mItemHeight));
            img.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mBridge != null && character.canClick()) {
                        DLIntent intent = new DLIntent("com.aube.plugin.origin", "com.aube.plugin.figure.CharacterActivity");
                        intent.putExtra(CharacterActivity.ACTOR, character);
                        mBridge.openPluginActivity(intent);
                    }
                }
            });
            ImageLoader.getInstance(context).fillImage(img, ImageSizeDefineUtil.getImgUrl(character.actorPic, i > 1 ? 400 : 600, i > 1 ? 400 : 600));
        }
    }

    @Override
    public void notifyAnim(int animDelay) {

    }

    @Override
    public void registToManager(IPositionManager iPositionManager) {

    }

    @Override
    public void unreistFromManager(IPositionManager iPositionManager) {

    }

    @Override
    public int[] getLocations() {
        return new int[0];
    }
}
